package com.ezcoding.controller;

import com.ezcoding.service.DingdanService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;

@Before(Tx.class)
public class DingdanController extends Controller {

    public void saveDingdan() {
        String dingdan = getPara("dingdan");
        String dingdanDetail = getPara("dingdanDetail");

        renderJson(DingdanService.getInstance().saveDingdan(dingdan, dingdanDetail));
    }

    public void deleteDingdan() {
        String username = getPara("username");
        String dingdanId = getPara("dingdanId");

        renderJson(DingdanService.getInstance().deleteDingdan(username, dingdanId));
    }

    public void getDingdans() {
        String username = getPara("username");
        int type = getParaToInt("type");
        int state = getParaToInt("state");

        renderJson(DingdanService.getInstance().getDingdans(username, type, state));
    }

    public void getDingdanDetails() {
        String username = getPara("username");
        String dingdanId = getPara("dingdanId");

        renderJson(DingdanService.getInstance().getDingdanDetails(username, dingdanId));
    }

    public void calculateDingdan() {
        String dingdan = getPara("dingdan");
        String dingdanDetail = getPara("dingdanDetail");

        renderJson(DingdanService.getInstance().calculateDingdan(dingdan, dingdanDetail));
    }

    public void queryDingdan() {
        String type = getPara("type");
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String startDate = getPara("startDate");
        String endDate = getPara("endDate");

        renderJson(DingdanService.getInstance().queryDingdan(type, sourceUsername, targetUsername, startDate, endDate));
    }

    public void queryDingdanByDetail() {
        String type = getPara("type");
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String productId = getPara("productId");
        String startDate = getPara("startDate");
        String endDate = getPara("endDate");

        renderJson(DingdanService.getInstance().queryDingdanByDetail(type, sourceUsername, targetUsername, productId, startDate, endDate));
    }

    public void getDingdanDetailsByType() {
        int type = getParaToInt("type");
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");

        renderJson(DingdanService.getInstance().getDingdanDetails(type, sourceUsername, targetUsername));
    }

    /**
     * 开票、收票、收款、付款
     */
    public void financeSubmit() {
        String companyId = getPara("companyId");
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String relatedMessageId = getPara("relatedMessageId");
        int relationType = getParaToInt("relationType");
        int financeType = getParaToInt("financeType");
        double curPreSave = Double.parseDouble(getPara("curPreSave"));
        double preSave = Double.parseDouble(getPara("preSave"));
        double totalFinanceMake = Double.parseDouble(getPara("totalFinanceMake"));
        String details = getPara("details");

        renderJson(DingdanService.getInstance().financeSubmit(
                companyId,
                sourceUsername,
                targetUsername,
                relatedMessageId,
                relationType,
                financeType,
                curPreSave,
                preSave,
                totalFinanceMake,
                details));
    }

    /**
     * 通过对方企业ID或我方商品ID查询历史价格记录
     */
    public void getHistoryProducts() {
        String sourceCompanyId = getPara("sourceCompanyId");
        String targetCompanyId = getPara("targetCompanyId");
        String sourceProductId = getPara("sourceProductId");
        //type 0:销售 1：采购
        int type = getParaToInt("type");

        renderJson(DingdanService.getInstance().getHistoryProducts(
                sourceCompanyId,
                targetCompanyId,
                sourceProductId,
                type));
    }

    /**
     * 获取导入订单
     */
    public void getImportDingdans() {
        String username = getPara("username");
        int state = getParaToInt("state");

        renderJson(DingdanService.getInstance().getImportDingdans(username, state));
    }

    /**
     * 获取导入订单详细信息
     */
    public void getImportDingdanDetails() {
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String sourceCompanyId = getPara("sourceCompanyId");
        String targetCompanyId = getPara("targetCompanyId");
        String dingdanId = getPara("dingdanId");
        //type 0:对方为客户，我方进行销售 1对方为供应商，我方进行采购
        int type = getParaToInt("type");

        renderJson(DingdanService.getInstance().getImportDingdanDetails(
                sourceUsername,
                targetUsername,
                sourceCompanyId,
                targetCompanyId,
                dingdanId,
                type));
    }
}
